package com.hsnn.medstgmini.trading.pur.controller;

import java.lang.reflect.Type;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.model.Status;
import com.hsnn.medstgmini.model.Whether;
import com.hsnn.medstgmini.sys.model.SysUser;
import com.hsnn.medstgmini.trading.pur.model.HospAddress;
import com.hsnn.medstgmini.trading.pur.service.AddressManager;
import com.hsnn.medstgmini.util.Pagination;

/**
 * @category 医疗机构收货地址管理
 * @author 蔡春龙
 * @date 2015年7月8日
 */
@Controller
@RequestMapping("/address")
public class AddressController {

	private static final Logger log = Logger.getLogger(AddressController.class);

	@Autowired
	private AddressManager addressManager;

	@Autowired
	private HttpServletRequest request;

	@Autowired
	private HttpSession session;

	/**
	 * @category 获取当前登陆人信息
	 * @date 2015年6月14日
	 * @return
	 */
	private SysUser getUser() {
		return (SysUser) session.getAttribute(Constants.USERINFO);
	}

	/**
	 * @category 进入地址管理页面
	 * @date 2015年7月8日
	 * @return
	 */
	@RequestMapping("/toAddress")
	public String toAddress() {
		return "/trading/pur/address";
	}

	/**
	 * @category 获取地址管理列表
	 * @date 2015年7月8日
	 * @param address
	 * @return
	 */
	@RequestMapping("/getAddressList")
	@ResponseBody
	public Pagination getAddressList(HospAddress address) {
		log.info("enter getAddressList method");
		Pagination page = new Pagination(request);
		try {
			SysUser user = getUser();
			addressManager.getAddressList(address, page, user);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 进入地址新增编辑页面
	 * @date 2015年7月8日
	 * @param model
	 * @param id
	 * @param opType
	 * @return
	 */
	@RequestMapping("/toAddressEdit")
	public String toAddressEdit() {		
		SysUser user = getUser();		
		HospAddress address = addressManager.getAddressByHospId(user.getOrgId());
		if (null == address){
			address = new HospAddress();
			address.setUser(user);
		}
		request.setAttribute("address", address);
		
		
		int opType=1;
		
		
		
	  /*  model.addAttribute("user", user);
		model.addAttribute("address", address);
		model.addAttribute("opType", opType);*/

		return "/trading/pur/addressEdit";
	}

	/**
	 * @category 保存地址
	 * @date 2015年7月8日
	 * @param address
	 * @return
	 */
	@RequestMapping("/saveAddress")
	@ResponseBody
	public Pagination saveAddress(HospAddress address) {
		log.info("enter saveAddress method");
		Pagination page = new Pagination(request);
		try {
			if (address != null) {
				SysUser user = getUser();
				if (user.getMain() == Whether.yes.getKey()) {
					// 医疗机构主帐号id即为医疗机构id
					address.setHospId(user.getOrgId());
				} else {
					// 医疗机构子帐号获取主帐号
					SysUser root = user.getRootUser();
					if (root != null) {
						address.setHospId(root.getOrgId());
					}
				}
				address.setStatus(Status.enabled.getKey());
				address.setUpdUser(user.getId());
				address.setUpdDateTime(new Date());
				addressManager.saveAddress(address);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 批量(启用|停用)
	 * @param array
	 * @return
	 */
	@RequestMapping("batchChangeStatus")
	@ResponseBody
	public Pagination batchChangeStatus(String array) {
		Pagination page = new Pagination();
		try {
			if (StringUtils.isNotBlank(array)) {
				Gson gson = new Gson();
				Type type = new TypeToken<List<HospAddress>>() {
				}.getType();
				List<HospAddress> list = gson.fromJson(array, type);
				if (list != null && !list.isEmpty()) {
					for (HospAddress address : list) {
						address.setUpdUser(getUser().getId());
					}
					addressManager.batchChangeStatus(list);
					page.setSuccess(true);
				}
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 设置默认地址
	 * @date 2015年7月8日
	 * @param address
	 * @return
	 */
	@RequestMapping("setDefaultAddress")
	@ResponseBody
	public Pagination setDefaultAddress(HospAddress address) {
		Pagination page = new Pagination();
		try {
			if (address != null) {
				address.setUpdUser(getUser().getId());
				addressManager.setDefaultAddress(address);
				page.setSuccess(true);
			}
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

	/**
	 * @category 进入用户列表页面
	 * @date 2015年7月8日
	 * @param model
	 * @return
	 */
	@RequestMapping("/toUserList")
	public String toUserList(Model model, String userIds) {
		model.addAttribute("userIds", userIds);
		return "/trading/pur/userList";
	}

	/**
	 * @category 获取用户列表
	 * @date 2015年7月8日
	 * @return
	 */
	@RequestMapping("getUserList")
	@ResponseBody
	public Pagination getUserList(SysUser user) {
		Pagination page = new Pagination();
		try {
			user = user == null ? new SysUser() : user;
			user.setId(getUser().getId());
			user.setMain(getUser().getMain());
			addressManager.getUserList(user, page);
			page.setSuccess(true);
		} catch (Exception e) {
			log.error("",e);
			page.setSuccess(false);
		}
		return page;
	}

}
